package Presenter;

import java.util.Map;

import Bean.CarBean;
import Bean.LoginBean;
import Bean.RegistBean;
import Bean.ShopDataBean;
import Bean.XiangBean;
import Model.IModel;
import Views.IView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class IPresenter implements IPre{
    private IView miv;
    private DisposableSubscriber subscriber1;
    private DisposableSubscriber subscriber2;
    private DisposableSubscriber subscriber3;
    private DisposableSubscriber subscriber4;
    private DisposableSubscriber subscriber5;

    public void attach(IView iv) {
        this.miv = iv;
    }
    public void detach() {
        if(miv != null) {
            miv = null;
        }
        if(subscriber1 != null) {
            if(subscriber1.isDisposed()) {
                subscriber1.dispose();
            }
        }
        if(subscriber2 != null) {
            if(subscriber2.isDisposed()) {
                subscriber2.dispose();
            }
        }
        if(subscriber3 != null) {
            if(subscriber3.isDisposed()) {
                subscriber3.dispose();
            }
        }
        if(subscriber4 != null) {
            if(subscriber4.isDisposed()) {
                subscriber4.dispose();
            }
        }
        if(subscriber5 != null) {
            if(subscriber5.isDisposed()) {
                subscriber5.dispose();
            }
        }
    }
    @Override
    public void getData(String url, Map<String, String> map) {
        IModel iModel = new IModel(this);
        iModel.getData(url,map);
        iModel.getData2(url,map);
        iModel.getData3(url,map);
        iModel.getData4(url,map);
        iModel.getData5(url,map);
    }

    public void setNewData1(Flowable<RegistBean> newData1) {
        subscriber1 = newData1.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<RegistBean>() {
                    @Override
                    public void onNext(RegistBean registBean) {
                        if(registBean != null) {
                            miv.onSuccess(registBean);
                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        miv.onFailed((Exception) t);
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }

    public void setNewData2(Flowable<LoginBean> newData2) {
        subscriber2 = newData2.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<LoginBean>() {
                    @Override
                    public void onNext(LoginBean loginbean) {
                        if(loginbean != null) {
                            miv.onSuccess(loginbean);
                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        miv.onFailed((Exception) t);
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }

    public void setNewData3(Flowable<ShopDataBean> newData3) {
        subscriber3 = newData3.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<ShopDataBean>() {
                    @Override
                    public void onNext(ShopDataBean shopdata) {
                        if(shopdata != null) {
                            miv.onSuccess(shopdata);
                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        miv.onFailed((Exception) t);
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }

    public void setNewData4(Flowable<XiangBean> newData4) {
        subscriber4 = newData4.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<XiangBean>() {
                    @Override
                    public void onNext(XiangBean xiang) {
                        if(xiang != null) {
                            miv.onSuccess(xiang);
                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        miv.onFailed((Exception) t);
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }

    public void setNewData5(Flowable<CarBean> newData5) {
        subscriber5 = newData5.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<CarBean>() {
                    @Override
                    public void onNext(CarBean cardata) {
                        if(cardata != null) {
                            miv.onSuccess(cardata);
                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        miv.onFailed((Exception) t);
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }
}
